# from django.http import JsonResponse
from backend.cews.models import EquipmentLevelRelation
from backend.cews.services.AssetService import create_asset_relation, soft_delete_asset_relation, update_asset_relation, import_asset_lv4_relation, import_asset_lv5_relation, import_asset_eq_relation
from drf_spectacular.utils import extend_schema
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.mixins import (CreateModelMixin,ListModelMixin,RetrieveModelMixin,UpdateModelMixin,)
from rest_framework.viewsets import GenericViewSet

from backend.cews.models import Asset
from utils.autoschema import AutoDocSchema
from utils.decorators import request_data, error_response, check_group_permission

from .serializers import AssetSerializer, EquipmentLevelRelationSerializer

class AssetViewSet(
    GenericViewSet,
    RetrieveModelMixin,
    ListModelMixin,
    # UpdateModelMixin,
    # CreateModelMixin
):
    serializer_class = AssetSerializer
    queryset = Asset.objects.all()

    @extend_schema(responses={200: AssetSerializer(many=True)})
    @action(detail=False, methods=["get"], url_name="create_asset",schema=AutoDocSchema())
    @request_data(keys=["level"], types={"level": str})
    def get_assets_by_level(self,request,level):
        '''
            desc: Get asset by level
            
            parameters:
                level:
                    type: number
         
        '''
        serializer = AssetSerializer(self.queryset.filter(level=int(level)), many=True)
        return Response(status=status.HTTP_200_OK, data=serializer.data)
    
    @action(detail=False, methods=["GET"], url_name="get_asset_relation",schema=AutoDocSchema())
    def get_asset_relation(self,request):
        queryset = EquipmentLevelRelation.objects.filter(deleted=False)
        serializer = EquipmentLevelRelationSerializer(queryset, many=True)
            
        return Response(status=status.HTTP_200_OK, data=serializer.data)



    @action(detail=False, methods=["POST"], url_name="create_asset_relation",schema=AutoDocSchema())
    @request_data(keys=["equipment_class", 'lv1', "lv2", "lv3", "lv4", "lv5"], 
                  optional=["lv5"]
                  )
    def create_asset_relation(self,request,equipment_class, lv1,lv2,lv3,lv4,lv5):
        '''
            desc: Create asset relation
            
            parameters:
                equipment_class:
                    type: string
                lv1:
                    type: string
                lv2:
                    type: string
                lv3:
                     type: string
                lv4:
                    type: string
                lv5:
                    type: string

        '''

        response = {}
        try:
            if create_asset_relation(equipment_class, lv1,lv2,lv3,lv4,lv5):
                response = {
                    "code": 1,
                    "message": "Success"
                }
            return Response(status=status.HTTP_200_OK, data=response)

        except ValueError as ex:
            return Response(status=status.HTTP_400_BAD_REQUEST, data={
                "code": 0,
                "message": str(ex)
            })
        
    @action(detail=False, methods=["PATCH"], url_name="update_asset_relation",schema=AutoDocSchema())
    @request_data(keys=["id","equipment_class", 'lv1', "lv2", "lv3", "lv4", "lv5"], 
                  optional=["lv5"]
                  )
    def update_asset_relation(self,request,id, equipment_class, lv1,lv2,lv3,lv4,lv5):
        '''
            desc: Update asset relation
            
            parameters:
                id:
                    type: number
                equipment_class:
                    type: string
                lv1:
                    type: string
                lv2:
                    type: string
                lv3:
                     type: string
                lv4:
                    type: string
                lv5:
                    type: string

        '''

        response = {}
        try:
            if update_asset_relation(id,equipment_class, lv1,lv2,lv3,lv4,lv5):
                response = {
                    "code": 1,
                    "message": "Success"
                }
            return Response(status=status.HTTP_200_OK, data=response)

        except ValueError as ex:
            return Response(status=status.HTTP_400_BAD_REQUEST, data={
                "code": 0,
                "message": str(ex)
            })
        
    @action(detail=False, methods=["POST"], url_name="get_cur_asset_relation",schema=AutoDocSchema())
    @request_data(keys=["id"])
    def get_cur_asset_relation(self,request,id):

        queryset = EquipmentLevelRelation.objects.get(pk=id)
 
        serializer = EquipmentLevelRelationSerializer(queryset, many=False)
            
        return Response(status=status.HTTP_200_OK, data=serializer.data)
    
    @action(detail=False, methods=["DELETE"], url_name="soft_delete_cur_asset_relation",schema=AutoDocSchema())
    @request_data(keys=["id"])
    @error_response()
    def soft_delete_cur_asset_relation(self,request,id):
        if soft_delete_asset_relation(
            id,
            request.user
        ):
            response = {
                "code": 1,
                "message": "Success delete asset relation"
            }
            
        return Response(status=status.HTTP_200_OK, data=response)
    
    @action(detail=False, methods=["POST"], url_name="import_asset_lv4_relation",schema=AutoDocSchema())
    @request_data(keys=["lv4"])
    @error_response()
    def import_asset_lv4_relation(self,request,lv4):

        queryset = import_asset_lv4_relation(lv4)
 
        serializer = EquipmentLevelRelationSerializer(queryset, many=False)
            
        return Response(status=status.HTTP_200_OK, data=serializer.data)
    
    @action(detail=False, methods=["POST"], url_name="import_asset_lv5_relation",schema=AutoDocSchema())
    @request_data(keys=["lv5"])
    @error_response()
    def import_asset_lv5_relation(self,request, lv5):

        queryset = import_asset_lv5_relation(lv5)
 
        serializer = EquipmentLevelRelationSerializer(queryset, many=False)
            
        return Response(status=status.HTTP_200_OK, data=serializer.data)
      
    @action(detail=False, methods=["POST"], url_name="import_asset_eq_relation",schema=AutoDocSchema())
    @request_data(keys=["equipment_class"])
    @error_response()
    def import_asset_eq_relation(self,request, equipment_class):

        queryset = import_asset_eq_relation(equipment_class)
 
        serializer = EquipmentLevelRelationSerializer(queryset, many=False)
            
        return Response(status=status.HTTP_200_OK, data=serializer.data)
    